* OVERVIEW
*This script generates tables and figures for the paper:
*"Genetic Endowments, Educational Outcomes, and the Moderating Influence of School Quality*" (by Arold, Hufe, Stoeckli)
*All raw data are stored in /rawdata
*All tables are outputted to /results/tables
*All figures are outputted to /results/figures

* SOFTWARE REQUIREMENTS
*Analyses run on Windows 11 using Stata version 18, R version 4.4.3, Python 3.8.19 through Windows PowerShell ISE (x86).

// ----------------
// Setting relevant paths (to be adjusted by replicator)
// ----------------
global ProjectDir "C:/Users/sn21763/OneDrive - University of Bristol/01 Research/HCF Genes/AHS_2025"
global pshell_path "C:/Windows/SysWOW64/WindowsPowerShell/v1.0/powershell_ise"
global R_path "C:/Program Files/R/R-4.4.3/bin/Rscript.exe"

// ----------------
// Record start time and initialize log
// ----------------
local datetime1 = clock("$S_DATE $S_TIME", "DMYhms")
clear
cap mkdir "${ProjectDir}/scripts/logs"
cap log close
local logdate : di %tcCCYY.NN.DD!_HH.MM.SS `datetime1'
local logfile "${ProjectDir}/scripts/logs/`logdate'.log.txt"
log using "`logfile'", text

// ----------------
// Run configuration file
// ----------------
do "${ProjectDir}/scripts/_config.do"

// ----------------
// Prepare data
// ----------------
do "${scripts}01_bartik_base.do"
do "${scripts}02_bartik_trend.do"
do "${scripts}03_bartik_consolidate.do"
rscript using "${scripts}04_individual variables.R", rpath(${R_path})
do "${scripts}05_pull school data.do"
do "${scripts}06_school assignment.do"
do "${scripts}07_school variables.do"

// ----------------
// Run analyses
// ----------------
do "${scripts}08_sample.do"
do "${scripts}09_representativity.do"
do "${scripts}10_analysis.do"
winexec "${pshell_path}" "${scripts}_pgi_correct.ps1"
*-> Remaining analyses using Python via Powershell ISE 
*-> The winexec command opens the relevant script in Powershell ISE 
*-> Replicators must change the path to their directory
*-> Replicators must run script by clicking green button, F5, or File -> Run

// ----------------
// Compile Exhibits
// ----------------
do "${scripts}11_tables.do"
do "${scripts}12_tables_supplementary.do"
do "${scripts}13_graphs.do"
do "${scripts}14_graphs_supplementary.do"
